package com.gun.management.module.raffle.controller.admin.lotterylog;

import com.gun.management.framework.security.core.LoginUser;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.gun.management.framework.common.pojo.PageParam;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.pojo.CommonResult;
import com.gun.management.framework.common.util.object.BeanUtils;
import static com.gun.management.framework.common.pojo.CommonResult.success;
import static com.gun.management.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

import com.gun.management.framework.excel.core.util.ExcelUtils;

//import com.gun.management.framework.operatelog.core.annotations.OperateLog;
//import static com.gun.management.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.gun.management.module.raffle.controller.admin.lotterylog.vo.*;
import com.gun.management.module.raffle.dal.dataobject.lotterylog.LotteryLogDO;
import com.gun.management.module.raffle.service.lotterylog.LotteryLogService;

@Tag(name = "管理后台 - 用户中奖日志")
@RestController
@RequestMapping("/raffle/lotterylog")
@Validated
public class LotteryLogController {

    @Resource
    private LotteryLogService lotteryLogService;

    @PostMapping("/create")
    // @Operation(summary = "创建用户中奖日志")
    @PreAuthorize("@ss.hasPermission('raffle:lotterylog:create')")
    public CommonResult<Long> createLotteryLog(@Valid @RequestBody LotteryLogSaveReqVO createReqVO) {
        return success(lotteryLogService.createLotteryLog(createReqVO));
    }

    @PutMapping("/update")
     @Operation(summary = "更新用户中奖日志")
    @PreAuthorize("@ss.hasPermission('raffle:lotterylog:update')")
    public CommonResult<Boolean> updateLotteryLog(@Valid @RequestBody LotteryLogSaveReqVO updateReqVO) {
        lotteryLogService.updateLotteryLog(updateReqVO);
        return success(true);
    }

    @PutMapping("/reSendingOrder")
    @Operation(summary = "用户推送更新奖品状态")
    @PreAuthorize("@ss.hasPermission('raffle:lotterylog:reSendingOrder')")
    public CommonResult<LotteryLogSendingOrderRespVO> reSendingOrder(@Valid @RequestBody LotteryLogUpdateReqVO updateReqVO) {
        return success(lotteryLogService.reSendingOrder(updateReqVO)).setMsg("用户重新推送订单");
    }

    @DeleteMapping("/delete")
     @Operation(summary = "删除用户中奖日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('raffle:lotterylog:delete')")
    public CommonResult<Boolean> deleteLotteryLog(@RequestParam("id") Long id) {
        lotteryLogService.deleteLotteryLog(id);
        return success(true);
    }

    @GetMapping("/get")
     @Operation(summary = "获得用户中奖日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('raffle:lotterylog:query')")
    public CommonResult<LotteryLogRespVO> getLotteryLog(@RequestParam("id") Long id) {
        LotteryLogDO lotteryLog = lotteryLogService.getLotteryLog(id);
        return success(BeanUtils.toBean(lotteryLog, LotteryLogRespVO.class));
    }

    @GetMapping("/page")
     @Operation(summary = "获得用户中奖日志分页")
    @PreAuthorize("@ss.hasPermission('raffle:lotterylog:page')")
    public CommonResult<PageResult<LotteryLogRespVO>> getLotteryLogPage(@Valid LotteryLogPageRequestVO pageReqVO) {
        LoginUser loginUser = getLoginUser();
        PageResult<LotteryLogRespVO> pageResult = lotteryLogService.getAdminLotteryLogPageList(loginUser.getId(),loginUser.getTenantId(),pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户中奖日志 Excel")
    @PreAuthorize("@ss.hasPermission('raffle:lotterylog:export')")
    public void exportLotteryLogExcel(@Valid LotteryLogPageRequestVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        LoginUser loginUser = getLoginUser();
        List<LotteryLogRespVO> list = lotteryLogService.getAdminLotteryLogPageList( loginUser.getId(),loginUser.getTenantId(),pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户中奖日志.xls", "数据", LotteryLogRespVO.class,
                        BeanUtils.toBean(list, LotteryLogRespVO.class));
    }

}